package com.bw.ivideoproject.login

import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentRegisterBinding
import com.bw.lib_mvvm.BaseFragment

class RegisterFragment : BaseFragment<FragmentRegisterBinding>() {

    val viewModel:LoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun bindLayout(): Int {
        return R.layout.fragment_register
    }

    override fun initView() {
    }

    override fun initData() {
        binding.register.setOnClickListener {
            val username = binding.username.text.toString()
            val password = binding.password.text.toString()
            if(username.isNullOrEmpty()){
                Toast.makeText(requireContext(),"username can't be empty", Toast.LENGTH_SHORT).show()
            }
            if(password.isNullOrEmpty()){
                Toast.makeText(requireContext(),"password can't be empty", Toast.LENGTH_SHORT).show()
            }
            if(!username.isNullOrEmpty() && !password.isNullOrEmpty()){
                viewModel.register(username, password)
            }
        }

    }

    override fun handleObserver() {
        super.handleObserver()
        viewModel.registerLiveData.observe(this){
            Toast.makeText(requireContext(),"register success", Toast.LENGTH_SHORT).show()
            val bundle = Bundle().apply {
                this.putString("username",binding.username.text.toString())
                this.putString("password",binding.password.text.toString())
            }
            (requireActivity() as LoginActivity).navFragment(R.id.loginFragment,bundle)
        }
    }
}